package com.lianniu.mgr.baseService.modifypassword.service.impl;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.modifypassword.dao.IModifyPasswordMapperDao;
import com.lianniu.mgr.baseService.modifypassword.model.ModifyPwdVo;
import com.lianniu.mgr.baseService.modifypassword.service.IModifyPasswordService;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import com.lianniu.mgr.util.MD5Util;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ModifyPasswordServiceImpl implements IModifyPasswordService {
	private final static Logger logger = Logger.getLogger(ModifyPasswordServiceImpl.class);

	@Resource
	IModifyPasswordMapperDao modifyPasswordMapperDao;

	/**
	 * @Description 修改密码
	 * @Param: mpVo
	 * @Return:BaseResult
	 * @author XuQing
	 * @date 2016年12月9日
	 */
	@Override
	@Transactional
	public BaseResult modifyPwd(ModifyPwdVo mpVo) {
		logger.info("ModifyPasswordServiceImpl modifyPwd begin.");
		logger.info("ModifyPasswordServiceImpl modifyPwd  oldpwd&原密码:" + JSON.toJSONString(mpVo.getOldPwd()));
		logger.info("ModifyPasswordServiceImpl modifyPwd  newpwd&新密码:" + JSON.toJSONString(mpVo.getNewPwd()));
		String staffId = StringUtil.nvl(mpVo.getStaffId());
		logger.info("ModifyPasswordServiceImpl modifyPwd 登录员工staffId:" + JSON.toJSONString(staffId));

		logger.info("ModifyPasswordServiceImpl getStaffInfo getStaffInfoByStaffId begin.");
		StaffVo staffVo = modifyPasswordMapperDao.getStaffInfoByStaffId(staffId);
		logger.info("ModifyPasswordServiceImpl getStaffInfo getStaffInfoByStaffId staffVo&登录员工信息："
				+ JSON.toJSONString(staffVo));
		logger.info("ModifyPasswordServiceImpl getStaffInfo getStaffInfoByStaffId end.");

		// 判断原密码是否正确
		String md5Pwd = staffVo.getLoginPassword();
		logger.info("ModifyPasswordServiceImpl modifyPwd md5Pwd&原密码的MD5码：" + JSON.toJSONString(md5Pwd));
		logger.info("ModifyPasswordServiceImpl modifyPwd checkPassword begin.");
		boolean oldpwdIsRight = MD5Util.checkPassword(mpVo.getOldPwd(), md5Pwd);
		logger.info("ModifyPasswordServiceImpl modifyPwd checkPassword oldpwdIsRight&原密码是否正确："
				+ JSON.toJSONString(oldpwdIsRight));
		logger.info("ModifyPasswordServiceImpl modifyPwd checkPassword end.");

		int modifyFlag = 0;
		// 如果原密码正确，则保存新密码
		if (oldpwdIsRight) {
			logger.info("ModifyPasswordServiceImpl modifyPwd saveNewPwd begin.");
			staffVo.setLoginPassword(MD5Util.getMD5String(mpVo.getNewPwd()));
			modifyFlag = modifyPasswordMapperDao.saveNewPwd(staffVo);
			logger.info(
					"ModifyPasswordServiceImpl modifyPwd saveNewPwd modifyFlag&保存标识：" + JSON.toJSONString(modifyFlag));
			logger.info("ModifyPasswordServiceImpl modifyPwd saveNewPwd end.");
		} else {
			return BaseResult.fail("oldPwdIsError", "密码输入错误，请重新输入！");
		}

		logger.info("ModifyPasswordServiceImpl modifyPwd end.");
		return BaseResult.success(modifyFlag);
	}

}
